package com.tesco.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tesco.dao.YgGoodsDao;
import com.tesco.entity.domin.YgGoods;
import com.tesco.service.YgGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author lx201
 */
@Service
public class YgGoodsServiceImpl extends ServiceImpl<YgGoodsDao, YgGoods> implements YgGoodsService {


    @Override
    public List<YgGoods> getUserGoodsList(YgGoods ygGoods) {
        QueryWrapper<YgGoods> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(ygGoods.getSpname())){
            queryWrapper.like(YgGoods.COL_SPNAME,ygGoods.getSpname());
        }
        List<YgGoods> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<YgGoods> getGoodsDetail(Integer id) {
        QueryWrapper<YgGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(YgGoods.COL_SPID,id);
        List<YgGoods> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<YgGoods> getStockByMerchant(String sjName) {
        QueryWrapper<YgGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(YgGoods.COL_SJNAME,sjName);
        List<YgGoods> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public Boolean updateStock(YgGoods ygGoods) {
        UpdateWrapper<YgGoods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(YgGoods.COL_KCNUM,ygGoods.getKcnum())
                .eq(YgGoods.COL_SPID,ygGoods.getSpid());
        return true;
    }

    @Override
    public YgGoods goodInfo(int spid) {
        QueryWrapper<YgGoods> ygGoodsQueryWrapper = new QueryWrapper<>();
        ygGoodsQueryWrapper.eq(YgGoods.COL_SPID,spid);
        return this.getOne(ygGoodsQueryWrapper);
    }

    @Override
    public Boolean subtractGoodNum(int num,int id) {
        QueryWrapper<YgGoods> ygGoodsQueryWrapper = new QueryWrapper<>();
        ygGoodsQueryWrapper.eq(YgGoods.COL_SPID,id);
        YgGoods one = this.getOne(ygGoodsQueryWrapper);
        if (one.getKcnum()<num){
            return false;
        }
        UpdateWrapper<YgGoods> ygGoodsUpdateWrapper = new UpdateWrapper<>();
        ygGoodsUpdateWrapper.set(YgGoods.COL_KCNUM,one.getKcnum()-num)
                .eq(YgGoods.COL_SPID,id);
        this.update(ygGoodsUpdateWrapper);
        return true;
    }

    @Override
    public Boolean addGoodNum(int num, int id) {
        QueryWrapper<YgGoods> ygGoodsQueryWrapper = new QueryWrapper<>();
        ygGoodsQueryWrapper.eq(YgGoods.COL_SPID,id);
        YgGoods one = this.getOne(ygGoodsQueryWrapper);
        UpdateWrapper<YgGoods> ygGoodsUpdateWrapper = new UpdateWrapper<>();
        ygGoodsUpdateWrapper.set(YgGoods.COL_KCNUM,one.getKcnum()+num)
                .eq(YgGoods.COL_SPID,id);
        this.update(ygGoodsUpdateWrapper);
        return true;
    }

    @Override
    public List<YgGoods> getGoodInfoList(String username) {
        QueryWrapper<YgGoods> ygGoodsQueryWrapper = new QueryWrapper<>();
        ygGoodsQueryWrapper.eq(YgGoods.COL_SJNAME,username);
        return this.list(ygGoodsQueryWrapper);
    }

}

